package com.geniusk.system.service.impl;

import com.geniusk.common.enums.ResponseType;
import com.geniusk.common.exception.GraceException;
import com.geniusk.common.utils.TreeUtil;
import com.geniusk.system.model.mapper.GroupMapper;
import com.geniusk.system.model.pojo.Group;
import com.geniusk.security.annotation.PreDataAuth;
import com.geniusk.common.enums.DataType;
import com.geniusk.system.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Override
    @PreDataAuth(groupAlias = "g", userAlias = "u")
    public List<Group> selectAllGroup(Group group) {
        List<Group> groupList = groupMapper.selectAllGroup(group);
        if(!groupList.isEmpty()){
            return buildGroup(groupList);
        }
        return new ArrayList<>();
    }

    @Override
    public int insertGroup(Group group) {
        Group info = groupMapper.selectGroupById(group.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if(info == null){
            group.setAncestors("0");
        } else if (!DataType.DISABLE.code().equals(info.getStatus())) {
            GraceException.display(ResponseType.DATA_DISABLE);
        } else {
            group.setAncestors(info.getAncestors() + "," + group.getParentId());
        }
        return groupMapper.insertGroup(group);
    }

    @Override
    public int updateGroup(Group group) {
        Group newParentGroup = groupMapper.selectGroupById(group.getParentId());
        Group oldGroup = groupMapper.selectGroupById(group.getGroupId());
        if (newParentGroup != null && oldGroup !=null) {
            String newAncestors = newParentGroup.getAncestors() + "," + newParentGroup.getGroupId();
            String oldAncestors = oldGroup.getAncestors();
            group.setAncestors(newAncestors);
            updateGroupChildren(group.getGroupId(), newAncestors, oldAncestors);
        }
        return groupMapper.updateGroup(group);
    }

    @Override
    public boolean hasChildByGroupId(Long groupId) {
        return groupMapper.hasChildByGroupId(groupId);
    }

    @Override
    public int deleteGroupById(Long groupId) {
        return groupMapper.deleteGroupById(groupId);
    }

    @Override
    public boolean selectGroupIsExistByGroupIds(Long[] groupIds) {
        return groupMapper.selectGroupIsExistByGroupIds(groupIds);
    }

    private List<Group> buildGroup(List<Group> groupList){
        List<Group> groupTree = new ArrayList<>();
        try{
            groupTree.addAll(TreeUtil.buildTree(groupList, "groupId"));
        }catch(Exception e){
            e.printStackTrace();
        }
        return groupTree;
    }

    /**
     * 修改子元素关系
     *
     * @param groupId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateGroupChildren(Long groupId, String newAncestors, String oldAncestors) {
        List<Group> children = groupMapper.selectGroupChildrenById(groupId);
        for (Group child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (!children.isEmpty()) {
            groupMapper.updateGroupChildren(children);
        }
    }
}
